Terminalen
OS X / Linux / Unix Command-Line in 15 Minutes Källa: http://introcs.cs.princeton.edu/java/15inout/unix.html Some Useful Commands *'javac': To compile a Java program from the command-line, use the javaccommand: javac HelloWorld.java *'more': Display the contents of a file one screenful at a time: more HelloWorld.java *'Examine a .class-file': A .class file is a binary file (sequence of 0s and 1s). If you are using Unix or OS X, you can examine its contents by typing od -x HelloWorld.class at the command prompt. This displays the results in hexadecimal (base 16). In deference to Java's name, the first word of every .class file is cafe. Working with Files and Directories Unix organizes files into a directory hierarchy. A directory can contain both files and other directories. They are completely analogous to folders in Windows. *'pwd': In order to find out in wich directory you are, type pwd at the Unix prompt: pwd *'ls': To view the contents of a directory, type ls. This command will list all the files and directories within the current directory. It is analogous to clicking on a Windows folder to see what's inside. :: ls :: Mail SunWS_config autosave HelloWorld.java public_html readme : There are 6 items in this directory. Some of them are files, like HelloWorld.java. Others are directories, like public_html. If you want to know which of these items are files and which are directories, you can use the command ls -F. *'ls-F:' The -F is called a flag. Many Unix commands have such flags that give you precise control over their behavior, in this case appending the '/' character to items that are directories: :: Mail/ SunWS_config/ autosave/ HelloWorld.java public_html/ readme.txt *'mkdir': To create a new directory, use the command mkdir. The following command creates a directory named hello: mkdir hello :: To see that it actually worked, use the ls command: ls -F *'mv': Now, move the two files HelloWorld.java and readme.txt into the hello directory using the mv command. :: mv HelloWorld.java hello mv readme.txt hello ls -F Mail/ SunWS_config/ autosave/ hello/ public_html/ The two files are no longer visible from the current directory. *'cd': To access the two files, change directories with the cd command. To confirm the directory change, you could use the pwd and ls commands. : To return back to your home directory, use the cd command, but this time with no directory name. *'cp': To make a copy of a file, use the cp command. The following command creates a backup copy of our HelloWorld.java program. This is especially useful when you modify a working program, but might want to revert back to the original version if your modifications don't succeed. :: cp HelloWorld.java HelloWorld.bak *'rm': Subsequently, you might want to clean up useless files. The rm command deletes a file. By default, you will have to confirm that you really want to delete a file by typing yes or y. The reason for this is that, unlike with Windows, deleted files are really deleted when you delete them. There is no Recycle Bin to act as a safety net. So be careful. *'wildcards': You can also apply the cp, rm, and mv commands to several files (or directories) at once. To create a new directory called loops, and copy all of the files in the directory /u/cos126/files/loops/ into this newly created directory type: :: mkdir loops cp /u/cos126/files/loops/* loops Here the * matches all files in the cos126/files/mandel directory. It copies them to your newly created mandel directory. Redirection Two important abstractions in Unix are standard input and standard output. By default standard input is your keyboard, and standard output is your monitor. Piping Another useful abstraction in Unix is piping. Piping is when the output of one program is used as the input of another program. For example, suppose we want to view the output of a program, but there is so much that it whizzes by on the screen too fast to read. (The program RandInts.java prints out a bunch of random integers.) One possible way to accomplish this is to type the following two commands. java RandInts > temp.txt more < temp.txt Note that more will work by redirecting the file temp.txt to standard input (as is done here) or by simply using the filename (as is done at the beginning of the document). Instead, we could do this in one line using the pipe symbol '|' java RandInts | more This is often useful when debugging a program, especially if your program goes into an infinite loop and you want to see the first few values that it prints. Kategori:IT Kategori:Programmering Kategori:Mac OS X